package com.sy.utils;

import com.alibaba.fastjson.JSONObject;
import com.sy.dto.ExpRecord;
import com.sy.dto.LoginRecord;
import com.sy.entity.LocateInfos;
import org.apache.commons.collections.CollectionUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zbbstart
 * @date 2021/1/15 - 9:46
 */
public class BilibiliUtils {
    private BilibiliUtils() {

    }

    /**
     * 获取当前年月日的字符串时间
     *
     * @return
     */
    public static String getCurdateString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        long millis = System.currentTimeMillis();
        Date date = new Date(millis);
        return sdf.format(date);
    }

    /**
     * 获取登录区域
     *
     * @return
     */
    public static String getLocate() {
        String url = "https://restapi.amap.com/v3/ip";
        Map<String, String> param = new HashMap<>();
        param.put("output", "JSON");
        param.put("key", "b24de133e93c51e42a2af99cc51f9ba7");
        String s = HttpClientUtil.doGet(url, param);
        JSONObject jsonObject = JSONObject.parseObject(s);
        LocateInfos locateInfos = (LocateInfos) JSONObject.toJavaObject(jsonObject, LocateInfos.class);
        String area = locateInfos.getProvince() + " " + locateInfos.getCity();
        return area;
    }

    /**
     * 获取本机当前IP地址
     *
     * @return
     */
    public static String getCurInetAddress() {
        String hostAddress = null;
        try {
            InetAddress address = InetAddress.getLocalHost();
            hostAddress = address.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return hostAddress;
    }

    /**
     * 设置当前登录记录
     * @param curdateString
     * @return
     */
    public static LoginRecord setLoginRecord(String curdateString) {
        LoginRecord loginRecord = new LoginRecord();
        loginRecord.setDate(curdateString);
        loginRecord.setChange(BilibiliUtils.getCurInetAddress());
        loginRecord.setLocate(BilibiliUtils.getLocate());
        return loginRecord;
    }

    /**
     * 设置当前经验值获取记录
     * @param curdateString
     * @param reason
     * @return
     */
    public static ExpRecord setExpRecord(String curdateString, String reason){
        ExpRecord expRecord = new ExpRecord();
        expRecord.setDate(curdateString);
        expRecord.setChange(BilibiliUtils.getCurInetAddress());
        expRecord.setReason(reason);
        return expRecord;
    }

    /**
     * 将json格式的字符串转为对象集合
     * @param list
     * @param clazz
     * @return
     */
    public static List toList(List<String> list, Class<?> clazz){
        ArrayList newList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)){
            for (String s : list) {
                JSONObject jsonObject = JSONObject.parseObject(s);
                Object o = JSONObject.toJavaObject(jsonObject, clazz);
                newList.add(o);
            }
        }
        return newList;
    }
}
